﻿using System;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.API.Basics.V1
{
    using LdCms.IBLL.Basics;
    using LdCms.Web.Services;
    /// <summary>
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("basics/district")]
    public class BasicsDistrictController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IHttpContextAccessor Accessor;
        private readonly IProvinceService ProvinceService;
        private readonly ICityService CityService;
        private readonly IAreaService AreaService;
        public BasicsDistrictController(IBaseApiManager BaseApiManager, IHttpContextAccessor Accessor, IProvinceService ProvinceService, ICityService CityService, IAreaService AreaService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.Accessor = Accessor;
            this.ProvinceService = ProvinceService;
            this.CityService = CityService;
            this.AreaService = AreaService;
        }
        [HttpGet]
        [ActionName("province")]
        public IActionResult GetProvince(string uuid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid);
                var lists = ProvinceService.GetProvince(SystemID);
                if (lists == null) { return Error("not data!"); }
                var data = from m in lists
                           select new
                           {
                               province_id = m.ProvinceID,
                               name = m.Name
                           };
                int total = data.Count();
                return Success(logId, "ok", new { total, rows = data });
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }
        [HttpGet]
        [ActionName("city")]
        public IActionResult GetCity(string uuid, int provinceid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid);
                var lists = CityService.GetCity(SystemID, provinceid);
                if (lists == null) { return Error("not data!"); }
                var data = from m in lists
                           select new
                           {
                               province_id = m.ProvinceID,
                               city_id = m.CityID,
                               name = m.Name
                           };
                int total = data.Count();
                return Success(logId, "ok", new { total, rows = data });
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }
        [HttpGet]
        [ActionName("area")]
        public IActionResult GetArea(string uuid,int cityid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid);
                var lists = AreaService.GetArea(SystemID, cityid);
                if (lists == null) { return Error("not data!"); }
                var data = from m in lists
                           select new
                           {
                               city_id = m.CityId,
                               area_id = m.AreaID,
                               name = m.Name
                           };
                int total = data.Count();
                return Success(logId, "ok", new { total, rows = data });
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }



    }
}